package com.sofa.client.controller;

import com.alipay.sofa.rpc.boot.runtime.param.BoltBindingParam;
import com.alipay.sofa.runtime.api.client.ClientFactory;
import com.alipay.sofa.runtime.api.client.ReferenceClient;
import com.alipay.sofa.runtime.api.client.param.BindingParam;
import com.alipay.sofa.runtime.api.client.param.ReferenceParam;
import com.sofa.client.AnnotationClientImpl;
import com.sofa.client.ClientAnnotatedBean;
import com.sofa.client.ClientFactoryBean;
import com.sofa.service.DirectService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author liyang
 * @description TODO
 * @date 2021/6/23 14:08
 */

@RestController
public class TestController {

    @Resource
    private AnnotationClientImpl annotationClientImpl;

    @Resource
    private ClientFactoryBean clientFactoryBean;

    @Resource
    private ClientAnnotatedBean clientAnnotatedBean;


    @GetMapping("/test/{name}")
    public String test(@PathVariable String name) {

        return annotationClientImpl.sayClientAnnotation(name);
    }


    @GetMapping("/test")
    public String test1() {

        ClientFactory clientFactory = clientFactoryBean.getClientFactory();
        ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
        ReferenceParam<DirectService> referenceParam = new ReferenceParam<DirectService>();
        referenceParam.setInterfaceType(DirectService.class);

        BindingParam refBindingParam = new BoltBindingParam();
        referenceParam.setBindingParam(refBindingParam);

        DirectService proxy = referenceClient.reference(referenceParam);
        String hello = proxy.sayDirect("hello");
        return hello;
    }

    @GetMapping("/test2/{name}")
    public String test2(@PathVariable String name) {

        // ClientFactory clientFactory = clientFactoryBean.getClientFactory();
        ClientFactory clientFactory = clientAnnotatedBean.getClientFactory();
        ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);

        ReferenceParam<DirectService> referenceParam = new ReferenceParam<DirectService>();
        referenceParam.setInterfaceType(DirectService.class);

        BindingParam refBindingParam = new BoltBindingParam();
        referenceParam.setBindingParam(refBindingParam);

        DirectService proxy = referenceClient.reference(referenceParam);
        String hello = proxy.sayDirect(name);
        System.out.println(hello);
        return hello;
    }


}
